<template>
  <div class="l-from-body learun-el-form-left" style="padding-left: 16px;">
    <el-form
      :model="formData"
      :rules="rules"
      size="mini"
      ref="form"
      labelPosition="left"
      label-width="80px"
    >
      <el-col :span="24">
        <el-form-item :label="$t('编码')" prop="f_DBName">
          <el-input v-model="formData.f_DBName"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item :label="$t('名称')" prop="f_DBAlias">
          <el-input v-model="formData.f_DBAlias"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item :label="$t('类型')" prop="f_DbType">
          <l-select
            :clearable="false"
            :options="options"
            v-model="formData.f_DbType"
            @change="handleTypeChange"
          ></l-select>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-tabs v-model="midActiveName" type="card">
          <el-tab-pane :label="$t('常规')" name="first">
            <template v-if="midActiveName == 'first'">
              <el-col :span="24">
                <el-form-item :label="$t('主机')" prop="f_Server">
                  <el-input v-model="formData.f_Server"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item :label="$t('端口')" prop="f_Port">
                  <el-input v-model="formData.f_Port"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item :label="$t('用户名')" prop="f_UserId">
                  <el-input v-model="formData.f_UserId"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item :label="$t('密码')" prop="f_Password">
                  <el-input v-model="formData.f_Password" type="password">
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item :label="$t('数据库')" prop="f_DataBase">
                  <el-input v-model="formData.f_DataBase"></el-input>
                </el-form-item>
              </el-col>
            </template>
          </el-tab-pane>
          <el-tab-pane :label="$t('高级')" name="second">
            <el-col v-if="midActiveName != 'first'" :span="24">
              <el-form-item :label="$t('连接串')" prop="f_DbConnection">
                <el-input v-model="formData.f_DbConnection"> </el-input>
              </el-form-item>
            </el-col>
          </el-tab-pane>
        </el-tabs>
      </el-col>
      <el-col :span="24">
        <el-form-item :label="$t('说明')" prop="f_Description">
          <el-input type="textarea" v-model="formData.f_Description"></el-input>
        </el-form-item>
      </el-col>
    </el-form>
  </div>
</template>
<script>
export default {
  props: {
    activeName: {
      type: String,
      default: () => "first",
    },
  },
  data() {
    return {
      formData: {
        f_DBName: "",
        f_DBAlias: "",
        f_DbType: "MySql",
        f_DbConnection: "server=localhost;Database=xxx;Uid=root;Pwd=haosql;SslMode=none;",
        f_Description: "",
        f_Server: "", //主机
        f_Port: "3306", //端口
        f_UserId: "", //用户名
        f_Password: "", //密码
        f_DataBase: "", //初始数据库
        f_Type: 0,
      },
      rules: {
        f_DBName: [
          { required: true, message: "请输入编码" },
          {
            validator: this.lr_existDbFiled,
            keyValue: () => {
              return this.formData.f_Id;
            },
            tableName: "lr_base_databaselink",
            keyName: "f_Id",
            trigger: "null",
          },
        ],
        f_DBAlias: [{ required: true, message: "请输入" }],
        f_DbType: [{ required: true, message: "请选择" }],
        f_Server: [{ required: true, message: "请输入" }],
        f_Port: [{ required: true, message: "请输入" }],
        f_UserId: [{ required: true, message: "请输入" }],
        f_Password: [{ required: true, message: "请输入" }],
        f_DataBase: [{ required: true, message: "请输入" }],
        f_DbConnection: [{ required: true, message: "请输入" }],
      },
      options: [
        { value: "MySql", label: "MySql" },
        { value: "SqlServer", label: "SqlServer" },
        { value: "Oracle", label: "Oracle" },
        { value: "PostgreSQL", label: "PostgreSQL" },
        { value: "Dameng", label: "达梦" },
        { value: "Kdbndp", label: "人大金仓" },
        { value: "HG", label: "瀚高" },
        { value: "Oscar", label: "神通" },
        { value: "OpenGauss", label: "华为OpenGauss" },
        { value: "OceanBase", label: "OceanBase" },
        { value: "Tidb", label: "Tidb" },
        { value: "GBase", label: "南大通用GBase" },
        { value: "TDengine", label: "TDengine" },
        { value: "ClickHouse", label: "ClickHouse" },
        { value: "QuestDB", label: "QuestDB" },
      ],
      link: {
        MySql:"server=localhost;Database=xxx;Uid=root;Pwd=haosql;SslMode=none;",
        SqlServer: "server=.;uid=sa;pwd=haosql;database=xxx",
        Oracle: "Data Source=localhost/orcl;User ID=system;Password=haha;",
        PostgreSQL:"PORT=5432;DATABASE=xxx;HOST=localhost;PASSWORD=haosql;USER ID=postgres",
        Dameng: "Server=localhost; User Id=SYSDBA; PWD=SYSDBA;DATABASE=xxx",
        Kdbndp:"Server=127.0.0.1;Port=54321;UID=SYSTEM;PWD=system;database=xxx",
        HG:"Server=127.0.0.1;Port=5866;UId=design;Password=000;Database=design;searchpath=design",
        Oscar:"PORT=2003;DATABASE=osrdb;HOST=localhost;PASSWORD=szoscar55;USER ID=SYSDBA",
        OpenGauss:"PORT=5432;DATABASE=SqlSugar4xTest;HOST=localhost;PASSWORD=haosql;USER ID=postgres;No Reset On Close=true",
        OceanBase:"server=localhost;Database=SqlSugar4xTest;Uid=root;Pwd=haosql;",
        Tidb:"server=localhost;Database=SqlSugar4xTest;Uid=root;Pwd=haosql;",
        GBase:"Driver={GBase ODBC DRIVER (64-Bit)};Host=localhost;Service=19088;Server=gbase01;Database=testdb;Protocol=onsoctcp;Uid=gbasedbt;Pwd=GBase123;Db_locale=zh_CN.utf8;Client_locale=zh_CN.utf8",
        TDengine:"Host=localhost;Port=6030;Username=root;Password=taosdata;Database=power",
        ClickHouse:"Database=SqlSugarTest2;Username=default;Password=;Host=localhost;Port=8123;Compression=False;UseSession=False;Timeout=120;",
        QuestDB:"host=localhost;port=8812;username=admin;password=quest;database=qdb;ServerCompatibilityMode=NoTypeLoading;",
      },
      port:{
        MySql:'3306',
        SqlServer:'1433',
        Oracle:'1521',
        PostgreSQL:'5432',
        Dameng:'5236',
        Kdbndp:'54321',
        HG:"5866",
        Oscar:"2003",
        OpenGauss:"5432",
        OceanBase:"26257",
        Tidb:"4000",
        GBase:"19088",
        TDengine:"6030",
        ClickHouse:"8123",
        QuestDB:"8812"
      }
    };
  },
  computed: {
    midActiveName: {
      get() {
        return this.activeName;
      },
      set(val) {
        this.$emit("update:activeName", val);
      },
    },
  },
  created() {},
  methods: {
    resetForm() {
      this.$formClear(this.$refs.form);
      this.formData.f_Id = "";
    },
    // 校验表单
    validateForm(callback) {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.formData.f_DBName == "lrsystemdb") {
            this.$message({
              type: "warning",
              message: this.$t("编码不可与系统数据库相同!"),
            });
            valid = false;
          }
        }
        callback(valid);
      });
    },
    setForm(data) {
      let formData = this.$deepClone(data);
      if (formData.f_DESEncrypt == 1) {
        // 解密
        if (formData.f_Type == 0) {
          formData.f_Password = this.lr_AESDecrypt(
            formData.f_Password,
            "learun_db"
          );
        } else {
          formData.f_DbConnection = this.lr_AESDecrypt(
            formData.f_DbConnection,
            "learun_db"
          );
        }
      }

      //this.dbConnection = formData.f_DbConnection;
      //formData.f_DbConnection = "**********";

      this.formData = formData;
    },
    getForm() {
      let formData = this.$deepClone(this.formData);
      formData.f_DESEncrypt = 1;
      if(this.activeName == 'first'){
        formData.f_Type = 0;
        formData.f_Password = this.lr_AESEncrypt(
          formData.f_Password,
          "learun_db"
        );
        formData.f_DbConnection = "";
      }
      else{
        formData.f_Type = 1;
        formData.f_DbConnection = this.lr_AESEncrypt(
          formData.f_DbConnection,
          "learun_db"
        );
        formData.f_Password = "";
      }
      return formData;
    },
    handleTypeChange() {
      this.formData.f_Port = this.port[this.formData.f_DbType];
      this.formData.f_DbConnection = this.link[this.formData.f_DbType];
    },
  },
};
</script>
